Simple values
For most of the types described there is a function to transform the Typescript version to the plu-ts
equivalent.
Here we cover the simple ones, leaving functions and structs to be covered later.
values constructors
plu-ts type | function name | ts to plu-ts function signature |
---|---|---|
unit | pmakeUnit | pmakeUnit(): Term<PUnit> |
int | pInt | pInt(x: number \ bigint): Term<PInt> |
bool | pBool | pBool(x: boolean): Term<PBool> |
bs | pByteString | pByteString(x: string \ ByteString \ Buffer): Term<PByteString> |
str | pStr | pStr(x: string): Term<PStr> |
data | pData | pData(x: Data): Term<PData> |
list | pList * | pList<T extends TermType>( t: T )( x: Term<ToPType<T>>[] ) |
pair | pPair ** | pPair<A extends TermType, B extends TermType>( fstT: A, sndT: B )( fst: Term<ToPType<A>>, snd: Term<ToPType<B>> ) |
delayed | *** not supported at ts level | *** explained below |
* pList
Since PList
is a generic type the pList
function has a slightly more complex function signature:
function pList<ElemsT extends TermType, PElemsT extends ToPType<ElemsT = ToPType<ElemsT>>>
( elemsT: ElemsT )
: ( elems: Term<PElemsT>[] ) => Term<PList<PElemsT>>
In the signature above, TermType
is the Typescript types of plu-ts
types (which are typescript values after all) and ToPType
is a utility type used internally and you should not worry about it.
From the signature we can already understand that given a plu-ts
type, pList
returns a function ad-hoc for terms of that type; so if we want a function to get list of integers we just do:
const pListInt: ( elems: Term<PInt>[] ) => Term<PList<PInt>> =
pList( int );
And with that we now have a function that transforms an array of terms into a list.
const intList = pListInt( [1,2,3,4].map( pInt ) );
You might notice that in contrast to the other functions introduced, pListInt
that we created works with terms instead of vanilla ts values; this is because pListInt
acts as a macro as seen by plu-ts
.
** pPair
Just like PList
, also PPair
is a generic type, which causes pPair
to have more complex function signature too:
function pPair<
A extends TermType,
B extends TermType,
PA extends ToPType<A> = ToPType<A>,
PA extends ToPType<B> = ToPType<B>,
>
( fstT: A, sndT: B )
: ( fst: PappArg<PA>, snd: PappArg<PB> ) => Term<PPair<PA,PB>>
and you would use it in a very similar way of pList
:
const myPair = pPair( bs, int )( "caffee", 2 )
Note how we are passing typescript value without transforming them to plu-ts
ones;
This is some magic done by 'plu-ts' so that if the type is known we don't have to explicitly construct it.
We'll see that this turns really useful while writing a smart contract
*** delayed
delayed
doesn't really have a Typescript value, so it only makes sense in the plu-ts
world.
You can only obtain a delayed value from an exsisting one using pdelay
function pdelay<PT extends PType>( someTerm: Term<PT> ): Term<PDelayed<PT>>